package com.stm.basicData.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.stm.basicData.domain.KfToolsBox;
import com.stm.basicData.mapper.KfToolsBoxMapper;
import com.stm.basicData.service.KfToolsBoxService;
import com.stm.common.annotation.DataScope;
import com.stm.common.core.text.Convert;
import com.stm.common.exception.BusinessException;
import com.stm.common.utils.DateUtils;
import com.stm.synchronize.domain.DeviceMain;
import com.stm.toolLedger.domain.KfTools;

/**
 * 
 * @Description RFIdimpl
 * @CreateTime 2019年4月4日 上午8:43:59
 * @CreateBy 林龙
 */
@Service
public class KfToolsBoxServiceImpl implements KfToolsBoxService
{
    @Autowired
    private KfToolsBoxMapper kfToolsBoxMapper;
    

    /**
     * 根据条件分页查询数据 
     * 
     * @param kfToolsBox 信息
     * @return list集合信息
     */
    @Override
    @DataScope(tableAlias = "b2")
    public List<KfToolsBox> selectAllPageList(KfToolsBox kfToolsBox){
        return kfToolsBoxMapper.selectAllPageList(kfToolsBox);
    }
    /**
     * 根据条件分页查询数据
     * 
     * @param kfToolsBox 信息
     * @return list集合信息
     */
    @Override
    public List<KfToolsBox> selectAllPagephoneList(KfToolsBox kfToolsBox){
    	return kfToolsBoxMapper.selectAllPagephoneList(kfToolsBox);
    }

    /**
     * 根据id查询信息
     * 
     * @param id 
     * @return 对象信息
     */
    @Override
    public KfToolsBox selectKfToolsBoxById(String id){
        return kfToolsBoxMapper.selectKfToolsBoxById(id);
    }

    /**
     * 新增保存信息
     * 
     * @param kfToolsBox RFID信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertKfToolsBox(KfToolsBox kfToolsBox){
    	return kfToolsBoxMapper.insertSelective(kfToolsBox);
    }

    /**
     * 修改保存信息
     * 
     * @param kfToolsBox RFID信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateKfToolsBox(KfToolsBox kfToolsBox){
    	return  kfToolsBoxMapper.updateByPrimaryKeySelective(kfToolsBox);
    }
    
    /**
     * 批量删除
     * 
     * @param ids
     * @throws Exception
     */
    @Override
    public int deleteKfToolsBoxByIds(String ids) throws BusinessException
    {
        String[] KfToolsBoxIds = Convert.toStringArray(ids);
        return kfToolsBoxMapper.deleteKfToolsBoxByIds(KfToolsBoxIds);
    }

    /**
     * 
     * @Description 根据库房名称查询库房信息
     * @CreateTime 2019年4月29日 上午8:59:52
     * @CreateBy 林龙
     * @param boxName
     * @return
     */
	@Override
	public KfToolsBox selectByBoxName(String boxName) {
		return kfToolsBoxMapper.selectByBoxName(boxName);
	}
	
	@DataScope(tableAlias="b2")
	@Override
	public List<KfToolsBox> getMapData(KfToolsBox kfToolsBox) {
		   return kfToolsBoxMapper.getMapData(kfToolsBox);
	}
	 
	@DataScope(tableAlias="b2")
	@Override
	public List<Map<String, Object>> getChartData(KfToolsBox kfToolsBox) {
		List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();	
		List<KfToolsBox> list1 = kfToolsBoxMapper.getMapData(kfToolsBox);
		int redNum=0;
		int greenNum=0;
		int yellowNum=0;
		int testNum=0;
		for (KfToolsBox kfToolsBox1 : list1) {
			if(kfToolsBox1.getStorehouseId().equals("1")||kfToolsBox1.getStorehouseId().equals("2")){
				if(kfToolsBox1.getDeviceMainList().size() == 0){ //红色
					redNum++;
				}else{
					boolean flag=false;
					for (DeviceMain deviceMain : kfToolsBox1.getDeviceMainList()) {
						
						Date updatetime= DateUtils.dateTime(DateUtils.YYYY_MM_DD,deviceMain.getUpdatetime());
						Date newdate= DateUtils.dateTime(DateUtils.YYYY_MM_DD,DateUtils.getDate());
						
						if(updatetime.equals(newdate) ){ 
							flag=true;
						}
					}
					if(flag){ //绿色
						greenNum++;
					}else{ //红色
						yellowNum++;
					}
				}
				
			}else{
				testNum++;
			}
				
		}
		
		Map<String,Object> resultMap1 = new HashMap<String,Object>();// 返回值
		resultMap1.put("name", "在线");
		resultMap1.put("value", greenNum);
		resultList.add(resultMap1);
		
		Map<String,Object> resultMap2 = new HashMap<String,Object>();// 返回值
		resultMap2.put("name", "暂离");
		resultMap2.put("value", yellowNum);
		resultList.add(resultMap2);
		
		Map<String,Object> resultMap = new HashMap<String,Object>();// 返回值
		resultMap.put("name", "离线");
		resultMap.put("value", redNum);
		resultList.add(resultMap);
		
		Map<String,Object> resultMap3 = new HashMap<String,Object>();// 返回值
		resultMap3.put("name", "试验");
		resultMap3.put("value", testNum);
		resultList.add(resultMap3);
		
		return resultList;
	}
	
	@DataScope(tableAlias="b2")
	@Override
	public List<Map<String, Object>> getLoadonline(KfToolsBox kfToolsBox) {
		List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();	
		List<KfToolsBox> list1 = kfToolsBoxMapper.selectAllChartData(kfToolsBox);
		
		for (KfToolsBox kfToolsBox1 : list1) {
				Map<String,Object> resultMap = new HashMap<String,Object>();// 返回值
				resultMap.put("name", kfToolsBox1.getDeptName());
				resultMap.put("value", kfToolsBox1.getNums());
				resultList.add(resultMap);
		}
		
		return resultList;
	}
	@Override
	public List<KfToolsBox> selectInitCity(KfToolsBox kfToolsBox) {
		return kfToolsBoxMapper.selectInitCity(kfToolsBox);
	}
	
	@DataScope(tableAlias="b2")
	@Override
	public List<KfToolsBox> getMapDataById(KfToolsBox kfToolsBox) {
		   return kfToolsBoxMapper.getMapDataById(kfToolsBox);
	}
	
}
